package au.edu.unsw.infs3634.gamifiedlearning.actiivty;

import androidx.annotation.NonNull;
import androidx.appcompat.app.ActionBar;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.RecyclerView;

import android.annotation.SuppressLint;
import android.graphics.Point;
import android.os.Build;
import android.os.Bundle;
import android.transition.Explode;
import android.view.Window;
import android.widget.AbsListView;
import android.widget.ListView;
import android.widget.RelativeLayout;

import com.shuyu.gsyvideoplayer.GSYVideoManager;
import com.shuyu.gsyvideoplayer.listener.GSYSampleCallBack;
import com.shuyu.gsyvideoplayer.utils.CommonUtil;
import com.shuyu.gsyvideoplayer.utils.Debuger;
import com.shuyu.gsyvideoplayer.utils.GSYVideoHelper;

import java.util.ArrayList;
import java.util.List;

import au.edu.unsw.infs3634.gamifiedlearning.R;
import au.edu.unsw.infs3634.gamifiedlearning.adapter.TrainVideoListAdapter;
import au.edu.unsw.infs3634.gamifiedlearning.model.VideoListModel;

public class TrainVideoListActivity extends AppCompatActivity {

    private static final String[] NAMES = new String[]{"消防安全(一)", "消防安全(二)", "消防安全(三)", "消防安全(四)", "消防安全(五)","消防安全(一)", "消防安全(二)", "消防安全(三)", "消防安全(四)", "消防安全(五)"};

    private static final int[] ICONS = new int[]{R.mipmap.ic_africa, R.mipmap.ic_asia, R.mipmap.ic_europe, R.mipmap.ic_america, R.mipmap.ic_oceania,R.mipmap.ic_africa, R.mipmap.ic_asia, R.mipmap.ic_europe, R.mipmap.ic_america, R.mipmap.ic_oceania};

    private static final String[] DATES = new String[]{"2022-10-2", "2022-12-1", "2023-1-1", "2023-1-6", "2022-1-8","2022-10-2", "2022-12-1", "2023-1-1", "2023-1-6", "2022-1-8"};

    private static final String[] URLS = new String[]{"https://vd3.bdstatic.com/mda-mm9cgpm1btwxhvbs/sc/cae_h264_nowatermark/1639127085506674619/mda-mm9cgpm1btwxhvbs.mp4?v_from_s=hkapp-haokan-hbe&auth_key=1675755081-0-0-502d6ec89d8427ec0680ff5b5b610ecc&bcevod_channel=searchbox_feed&cd=0&pd=1&pt=3&logid=0081275851&vid=10167691584334805284&abtest=106847_2-107354_2&klogid=0081275851", "https://vd2.bdstatic.com/mda-mmmc9j778ruvzsgh/sc/cae_h264_nowatermark/1640164717345903261/mda-mmmc9j778ruvzsgh.mp4?v_from_s=hkapp-haokan-hbe&auth_key=1675755147-0-0-7db0acb063f84527131544b7ab6e70fa&bcevod_channel=searchbox_feed&cd=0&pd=1&pt=3&logid=0147288184&vid=15243723544448234846&abtest=106847_2-107354_2&klogid=0147288184", "https://vd3.bdstatic.com/mda-nmd4584d8bkwfm6h/720p/h264/1670986629944304865/mda-nmd4584d8bkwfm6h.mp4?v_from_s=hkapp-haokan-hbe&auth_key=1675755205-0-0-070fa3f48810c50c709c2b217722f1d5&bcevod_channel=searchbox_feed&cd=0&pd=1&pt=3&logid=0205317511&vid=11990721039116275194&abtest=106847_2-107354_2&klogid=0205317511", "https://vd2.bdstatic.com/mda-kmhnnjeajaxv5rm0/v1-cae/sc/mda-kmhnnjeajaxv5rm0.mp4?v_from_s=hkapp-haokan-hbe&auth_key=1675755254-0-0-77e809a7b3c43ef8dd9620bf71e34672&bcevod_channel=searchbox_feed&cd=0&pd=1&pt=3&logid=0254850336&vid=16153086648451305029&abtest=106847_2-107354_2&klogid=0254850336", "https://vd2.bdstatic.com/mda-nh44u1y56ijiwevn/cae_h264/1659678042179636007/mda-nh44u1y56ijiwevn.mp4?v_from_s=hkapp-haokan-hbe&auth_key=1675755300-0-0-3c35db44d54a73a9c61f25f4c0d92188&bcevod_channel=searchbox_feed&cd=0&pd=1&pt=3&logid=0300893901&vid=3738877011591362803&abtest=106847_2-107354_2&klogid=0300893901","https://vd3.bdstatic.com/mda-mm9cgpm1btwxhvbs/sc/cae_h264_nowatermark/1639127085506674619/mda-mm9cgpm1btwxhvbs.mp4?v_from_s=hkapp-haokan-hbe&auth_key=1675755081-0-0-502d6ec89d8427ec0680ff5b5b610ecc&bcevod_channel=searchbox_feed&cd=0&pd=1&pt=3&logid=0081275851&vid=10167691584334805284&abtest=106847_2-107354_2&klogid=0081275851", "https://vd2.bdstatic.com/mda-mmmc9j778ruvzsgh/sc/cae_h264_nowatermark/1640164717345903261/mda-mmmc9j778ruvzsgh.mp4?v_from_s=hkapp-haokan-hbe&auth_key=1675755147-0-0-7db0acb063f84527131544b7ab6e70fa&bcevod_channel=searchbox_feed&cd=0&pd=1&pt=3&logid=0147288184&vid=15243723544448234846&abtest=106847_2-107354_2&klogid=0147288184", "https://vd3.bdstatic.com/mda-nmd4584d8bkwfm6h/720p/h264/1670986629944304865/mda-nmd4584d8bkwfm6h.mp4?v_from_s=hkapp-haokan-hbe&auth_key=1675755205-0-0-070fa3f48810c50c709c2b217722f1d5&bcevod_channel=searchbox_feed&cd=0&pd=1&pt=3&logid=0205317511&vid=11990721039116275194&abtest=106847_2-107354_2&klogid=0205317511", "https://vd2.bdstatic.com/mda-kmhnnjeajaxv5rm0/v1-cae/sc/mda-kmhnnjeajaxv5rm0.mp4?v_from_s=hkapp-haokan-hbe&auth_key=1675755254-0-0-77e809a7b3c43ef8dd9620bf71e34672&bcevod_channel=searchbox_feed&cd=0&pd=1&pt=3&logid=0254850336&vid=16153086648451305029&abtest=106847_2-107354_2&klogid=0254850336", "https://vd2.bdstatic.com/mda-nh44u1y56ijiwevn/cae_h264/1659678042179636007/mda-nh44u1y56ijiwevn.mp4?v_from_s=hkapp-haokan-hbe&auth_key=1675755300-0-0-3c35db44d54a73a9c61f25f4c0d92188&bcevod_channel=searchbox_feed&cd=0&pd=1&pt=3&logid=0300893901&vid=3738877011591362803&abtest=106847_2-107354_2&klogid=0300893901"};

    private ListView rvList;

    private TrainVideoListAdapter listVideoAdapter;

    private GSYVideoHelper smallVideoHelper;

    private GSYVideoHelper.GSYVideoHelperBuilder gsySmallVideoHelperBuilder;

    int lastVisibleItem;

    int firstVisibleItem;
    private RelativeLayout activityListVideo;

    @SuppressLint({"MissingInflatedId", "ObsoleteSdkInt"})
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // 设置一个exit transition
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            getWindow().requestFeature(Window.FEATURE_CONTENT_TRANSITIONS);
            getWindow().setEnterTransition(new Explode());
            getWindow().setExitTransition(new Explode());
        }
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_train_video_list_activity);
        initView();

    }

    private void initView() {
        ActionBar actionBar = getSupportActionBar();
        actionBar.setDisplayHomeAsUpEnabled(true);
        setTitle("培训视频");
        rvList = findViewById(R.id.video_list);
        activityListVideo = (RelativeLayout) findViewById(R.id.activityListVideo);
        //创建小窗口帮助类
        smallVideoHelper = new GSYVideoHelper(this);

        //配置
        gsySmallVideoHelperBuilder = new GSYVideoHelper.GSYVideoHelperBuilder();
        gsySmallVideoHelperBuilder
                .setHideStatusBar(true)
                .setNeedLockFull(true)
                .setCacheWithPlay(true)
                .setShowFullAnimation(false)
                .setRotateViewAuto(false)
                .setLockLand(true)
                .setVideoAllCallBack(new GSYSampleCallBack(){
                    @Override
                    public void onPrepared(String url, Object... objects) {
                        super.onPrepared(url, objects);
                        Debuger.printfLog("Duration " + smallVideoHelper.getGsyVideoPlayer().getDuration() + " CurrentPosition " + smallVideoHelper.getGsyVideoPlayer().getCurrentPositionWhenPlaying());
                    }

                    @SuppressLint("NotifyDataSetChanged")
                    @Override
                    public void onQuitSmallWidget(String url, Object... objects) {
                        super.onQuitSmallWidget(url, objects);
                        //大于0说明有播放,//对应的播放列表TAG
                        if (smallVideoHelper.getPlayPosition() >= 0 && smallVideoHelper.getPlayTAG().equals(TrainVideoListAdapter.TAG)) {
                            //当前播放的位置
                            int position = smallVideoHelper.getPlayPosition();
                            //不可视的是时候
                            if ((position < firstVisibleItem || position > lastVisibleItem)) {
                                //释放掉视频
                                smallVideoHelper.releaseVideoPlayer();
                                listVideoAdapter.notifyDataSetChanged();
                            }
                        }

                    }
                });

        smallVideoHelper.setGsyVideoOptionBuilder(gsySmallVideoHelperBuilder);

        listVideoAdapter = new TrainVideoListAdapter(this, getVideoList(),smallVideoHelper, gsySmallVideoHelperBuilder);
        listVideoAdapter.setRootView(activityListVideo);
        rvList.setAdapter(listVideoAdapter);
        rvList.setOnScrollListener(new AbsListView.OnScrollListener() {
            @Override
            public void onScrollStateChanged(AbsListView view, int scrollState) {
            }

            @Override
            public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
                TrainVideoListActivity.this.firstVisibleItem = firstVisibleItem;
                lastVisibleItem = firstVisibleItem + visibleItemCount;
                //大于0说明有播放,//对应的播放列表TAG
                if (smallVideoHelper.getPlayPosition() >= 0 && smallVideoHelper.getPlayTAG().equals(TrainVideoListAdapter.TAG)) {
                    //当前播放的位置
                    int position = smallVideoHelper.getPlayPosition();
                    //不可视的是时候
                    if ((position < firstVisibleItem || position > lastVisibleItem)) {
                        //如果是小窗口就不需要处理
                        if (!smallVideoHelper.isSmall()) {
                            //小窗口
                            int size = CommonUtil.dip2px(TrainVideoListActivity.this, 150);
                            smallVideoHelper.showSmallVideo(new Point(size, size), false, true);
                        }
                    } else {
                        if (smallVideoHelper.isSmall()) {
                            smallVideoHelper.smallVideoToNormal();
                        }
                    }
                }
            }

        });
    }

    private List<VideoListModel> getVideoList() {
        List<VideoListModel> list = new ArrayList<>();
        for (int i = 0; i < NAMES.length; i++) {
            VideoListModel model = new VideoListModel(NAMES[i], ICONS[i], DATES[i], URLS[i]);
            list.add(model);
        }
        return list;
    }

    @Override
    public void onBackPressed() {
        if (smallVideoHelper.backFromFull()) {
            return;
        }
        super.onBackPressed();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        smallVideoHelper.releaseVideoPlayer();
        GSYVideoManager.releaseAllVideos();
    }

    //Back button
    @Override
    public boolean onSupportNavigateUp() {
        finish();
        return true;
    }
}